﻿namespace Web.Control.Member.My
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;
    using System.Collections;
    using System.Net;
    using System.Collections.Generic;
    using Lx.Web.Common.UI;
    using Lx.Web.Common.ControlsUI;   
    //using Wuqi.Webdiyer;   
    using System.IO;
    using System.Text;
using Model.WWW_User.Teach;


    public class Default : BaseControl
    {
        protected Repeater rptComment;
        protected Repeater rptMember;
        protected Repeater rptLastEvent;
        /// <summary>
        /// 数量集合 int list(0:好友请求数 1:未读通知数 2:未读消息数 3:选择最佳答案数 4:sesson请求数 5:小黑板数量)
        /// </summary>
        protected List<int> Countlist = new List<int>();
        /// <summary>
        /// 当前用户类型"1"学生 "2"老师
        /// </summary>
        protected string UserType = "1";
        /// <summary>
        /// 我的信息
        /// </summary>
        protected Model.WWW_User.User.UserSummaryInfoModel MyInfo;
        /// <summary>
        /// 我的session列表
        /// </summary>
        private List<SessionsModel> SessionList;
        /// <summary>
        /// 其他信息ArrayList(0:decimal教师平级、1:老师所教语言(当为学生为""))
        /// </summary>
        protected ArrayList OtherArr;
        /// <summary>
        /// 当前会员号
        /// </summary>
        private string MyMemberNo = "";

        protected string C0033 = "";
        protected string C0035 = "";
        /// <summary>
        /// 支付连接
        /// </summary>
        string payUrl = "";

        protected override void OnLoad(EventArgs e)
        {
            MyMemberNo = HttpContext.Current.User.Identity.Name;
            if (MyMemberNo == "")
            {
                Page.Response.Redirect("/user/login.htm?ReturnUrl=" + Page.Request.Url.AbsoluteUri);
                return;
            }
            C0033 = GetText("C0033");
            C0035 = GetText("C0035");
            payUrl = System.Configuration.ConfigurationManager.AppSettings["PayDetail"];

            ArrayList arrList = BLF.WWW_User.Users.User.GetMyHomePage(MyMemberNo);
            if (arrList != null)
            {
                UserType = arrList[0].ToString();
                Countlist = arrList[1] as List<int>;
                MyInfo = arrList[2] as Model.WWW_User.User.UserSummaryInfoModel;
                SessionList = arrList[3] as List<SessionsModel>;
                OtherArr = arrList[7] as ArrayList;
                if (rptMember != null)
                {
                    rptMember.DataSource = arrList[4];
                    rptMember.DataBind();
                }
                if (rptLastEvent != null)
                {
                    rptLastEvent.DataSource = arrList[5];
                    rptLastEvent.DataBind();
                }
                if (rptComment != null)
                {
                    rptComment.DataSource = arrList[6];
                    rptComment.DataBind();
                }
            }
            else {
                Response.End();
                return;
            }
            base.OnLoad(e);
        }
        private List<SessionsModel> SortSessionList(List<SessionsModel> sList)
        {
            List<SessionsModel> sessionS1 = new List<SessionsModel>();
            List<SessionsModel> sessionS2 = new List<SessionsModel>();
            List<SessionsModel> sessionT1 = new List<SessionsModel>();
            List<SessionsModel> sessionT2 = new List<SessionsModel>();
            List<SessionsModel> sessionS = new List<SessionsModel>();
            List<SessionsModel> sessionT = new List<SessionsModel>();

            DateTime CurDTime = DateTime.Now;
            foreach (SessionsModel sm in sList)
            {
                switch (sm.State)
                {
                    case "5"://付款
                    case "8"://老师声明此课时无效,学生是否同意
                    case "B"://学生还未确认课时,老师已确认上完课
                        if (UserType != "2" && sm.TNO != MyMemberNo)
                        {//icon_response.gif
                            sm.AudioType = "icon_response.gif";
                            sm.CountMsg = (int)((TimeSpan)(CurDTime - sm.HappenDate)).TotalMinutes;                            
                            sessionS.Add(sm);
                        }
                        break;
                    case "6"://
                        DateTime dt = BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, "480");
                        if (dt < CurDTime && dt.AddMinutes((double)(sm.LongTime * 30)) > CurDTime)//正在上课的 icon_live.gif
                        {
                            sm.AudioType = "icon_live.gif";
                            sm.CountMsg = (int)((TimeSpan)(CurDTime - dt)).TotalMinutes;
                            if (UserType == "2")
                                sessionT1.Add(sm);
                            else
                                sessionS1.Add(sm);       
                        }
                        else
                        {//即将上课的icon_upcoming.gif
                            sm.AudioType = "icon_upcoming.gif";
                            sm.CountMsg = (int)((TimeSpan)(dt - CurDTime)).TotalMinutes;
                            if (UserType == "2")
                                sessionT2.Add(sm);
                            else
                                sessionS2.Add(sm);                            
                        }
                        break;
                    case "7"://课时已结束，老师还未确认                                
                    case "C"://学生申请退款 老师解释
                        if (UserType == "2" && sm.TNO == MyMemberNo)
                        {
                            sm.AudioType = "icon_response.gif";
                            sm.CountMsg = (int)((TimeSpan)(CurDTime - sm.HappenDate)).TotalMinutes;
                            sessionT.Add(sm);
                        }
                        break;                    
                        break;
                }
            }
            sessionS1.Sort(sortCompar);
            sessionS2.Sort(sortCompar);
            sessionT1.Sort(sortCompar);
            sessionT2.Sort(sortCompar);
            sessionS.Sort(sortCompar);
            sessionT.Sort(sortCompar);

            sessionT1.AddRange(sessionS1);
            sessionT1.AddRange(sessionT2);
            sessionT1.AddRange(sessionS2);
            sessionT1.AddRange(sessionT);
            sessionT1.AddRange(sessionS);
            return sessionT1;
        }
        /// <summary>
        /// 得到更新的内容
        /// </summary>
        /// <param name="CateId">文字id</param>
        /// <param name="VarReplace">替换的字</param>
        /// <returns>string 更新的内容</returns>
        protected String GetUserLastEventDescrip(String CateId, String VarReplace)
        {

            if (CateId != null && VarReplace != null)
            {
                String[] StrArray = VarReplace.Split('\n');
                if (StrArray != null)
                {
                    return GetText(CateId.Trim(), StrArray);
                }
                else
                {
                    return "";
                }
            }
            return "";
        }
       
        protected string GetMySession()
        { 
            List<SessionsModel> myOpSList = new List<SessionsModel>();//我操作的
            List<SessionsModel> anotherOpSList = new List<SessionsModel>();//对方操作的
            List<SessionsModel> inClassSList = new List<SessionsModel>();//正在上课的
            List<SessionsModel> beClassSList = new List<SessionsModel>();//即将上课的
            List<SessionsModel> otherSList = new List<SessionsModel>();//其他
            string TP141 = GetText("TP141");//5
            string ViewSession = GetText("TP136");//6
            string oneToOne = GetText("TP250");            
            string TP946 = GetText("TP946");
            string TP947 = GetText("TP947");//
            string willTutorText = GetText("TP948");
            string sp7 = GetText("TP949");//老师确认课时
            string op7 = GetText("TP142");
            string op7c = GetText("TP143");
            string spB = GetText("TP950");//学生确认课时
            string opB = op7;
            string opBc = GetText("TP144");
            string spC = GetText("TP951");
            string opC = GetText("TP145");
            string sp8 = GetText("TP952");
            string op8 = GetText("TP112");
            string op8c = GetText("TP113");
            string TP354 = GetText("TP354");
            string TP353 = GetText("TP353");
            string TP352 = GetText("TP352");
            string TP407 = GetText("TP407");

            DateTime CurDTime = DateTime.Now;
            //0:图片名称、1会员号、2是否教师、3昵称、4标题、5时间提示、6操作
            string concent = "<li class='right_tp_ul'><div class='floatl' style='width:20px'>{0}</div><div class='floatl'> <span class='text14'><a href='/{1}{2}.htm'>{3}</a> - {4}</span><p><ul class='sortby'><li class='text'>{5}</li>{6}</ul></p></div><div class='clear'></div></li>";
            string imgResponse = "<img src='/Images/icon_response.gif'/>";
            string imgLive = "<img src='/Images/icon_live.gif'/>";
            string imgUpcoming = "<img src='/Images/icon_upcoming.gif'/>";

            string hrUrl = "";//第一个操作连接
            string hrView = "";//查看session连接 
            string title = "";
            foreach (SessionsModel sm in SessionList)
            {
                hrView = string.Format("<li class='last'><a href='/session/session.htm?sid={0}'>{1}</a></li>", sm.SessionID, ViewSession);
                title = sm.SessionType == "1" ? string.Format("<a href='/lesson/{0}'>{1} {2}</a>", sm.LID.ToString(), Lx.Web.Common.UI.Globals.HtmlStrippedText(sm.CTitle), string.Format(TP407, sm.LChapter, Lx.Web.Common.UI.Globals.HtmlStrippedText(sm.LTitle))) : oneToOne;
                switch (sm.State)
                {
                    case "3":
                    case "5"://付款
                        DateTime dt1 = BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, "480");
                        if (dt1 > CurDTime)
                        {
                            if (CurDTime.AddHours(24) < dt1)
                                dt1 = sm.HappenDate.AddHours(24);
                            sm.CountMsg = (int)((TimeSpan)(dt1 - CurDTime)).TotalMinutes;
                            if (sm.SNO == MyMemberNo)
                            {//icon_response.gif
                                //hrUrl = string.Format("<li><a href='{0}'>{1}</a></li>{2}", string.Format(payUrl, sm.SessionID), TP141, hrView);
                                hrUrl = string.Format("<li><a href='/session/session.htm?sid={0}'>{1}</a></li>{2}", sm.SessionID, TP141, hrView);
                                sm.BaseLanguage = string.Format(concent, imgResponse, sm.TNO, "T", sm.TNickname, title, GetDateTimeFormat(TP946, dt1, CurDTime), hrUrl);
                                myOpSList.Add(sm);
                            }
                            else if (sm.TNO == MyMemberNo)
                            {
                                sm.BaseLanguage = string.Format(concent, imgResponse, sm.SNO, "", sm.SNickname, title, GetDateTimeFormat(TP946, dt1, CurDTime), hrView);
                                anotherOpSList.Add(sm);
                            }
                        }
                        break;
                    case "6":
                        DateTime dt = BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, "480");
                        if (dt < CurDTime && dt.AddMinutes((double)(sm.LongTime * 30)) > CurDTime)//正在上课的 icon_live.gif
                        {
                            sm.CountMsg = (int)((TimeSpan)(CurDTime - dt)).TotalMinutes;
                            if (sm.TNO == MyMemberNo)
                            {
                                sm.BaseLanguage = string.Format(concent, imgLive, sm.SNO, "", sm.SNickname, title, GetDateTimeFormat(TP947, CurDTime, dt), hrView);
                            }
                            else
                            {
                                sm.BaseLanguage = string.Format(concent, imgLive, sm.TNO, "T", sm.TNickname, title, GetDateTimeFormat(TP947, CurDTime, dt), hrView);
                            }
                            inClassSList.Add(sm);
                        }
                        else if (dt > CurDTime)
                        {//即将上课的icon_upcoming.gif
                            sm.CountMsg = (int)((TimeSpan)(dt - CurDTime)).TotalMinutes;
                            if (sm.TNO == MyMemberNo)
                            {
                                sm.BaseLanguage = string.Format(concent, imgUpcoming, sm.SNO, "", sm.SNickname, title, GetDateTimeFormat(willTutorText, dt, CurDTime), hrView);
                            }
                            else
                            {
                                sm.BaseLanguage = string.Format(concent, imgUpcoming, sm.TNO, "T", sm.TNickname, title, GetDateTimeFormat(willTutorText, dt, CurDTime), hrView);
                            }
                            beClassSList.Add(sm);
                        }
                        break;
                    case "7"://课时已结束，老师还未确认
                        if (sm.HappenDate.AddDays(3) > CurDTime)
                        {
                            sm.CountMsg = (int)((TimeSpan)(sm.HappenDate.AddDays(3) - CurDTime)).TotalMinutes;
                            if (sm.TNO == MyMemberNo)
                            {
                                //hrUrl = string.Format("<li><a href='/session/sessionstateb.htm?sid={0}'>{1}</a></li><li><a href='/session/sessionstate8.htm?sid={0}'>{2}</a></li>{3}", sm.SessionID, op7, op7c, hrView);
                                hrUrl = string.Format("<li><a href='/session/session.htm?sid={0}'>{1}</a></li><li><a href='/session/session.htm?sid={0}'>{2}</a></li>{3}", sm.SessionID, op7, op7c, hrView);
                                sm.BaseLanguage = string.Format(concent, imgResponse, sm.SNO, "", sm.SNickname, title, GetDateTimeFormat(sp7, sm.HappenDate.AddDays(3), CurDTime), hrUrl);
                                myOpSList.Add(sm);
                            }
                            else
                            {
                                sm.BaseLanguage = string.Format(concent, imgResponse, sm.TNO, "T", sm.TNickname, title, GetDateTimeFormat(sp7, sm.HappenDate.AddDays(3), CurDTime), hrView);
                                anotherOpSList.Add(sm);
                            }
                        }
                        break;
                    case "8"://老师声明此课时无效,学生是否同意
                        if (sm.HappenDate.AddDays(7) > CurDTime)
                        {
                            sm.CountMsg = (int)((TimeSpan)(sm.HappenDate.AddDays(7) - CurDTime)).TotalMinutes;
                            if (sm.SNO == MyMemberNo)
                            {
                                //hrUrl = string.Format("<li><a href='/session/sessionstate9a.htm?sid={0}'>{1}</a></li><li><a href='/session/sessionstate9a.htm?sid={0}'>{2}</a></li>{3}", sm.SessionID, op8, op8c, hrView);
                                hrUrl = string.Format("<li><a href='/session/session.htm?sid={0}'>{1}</a></li><li><a href='/session/session.htm?sid={0}'>{2}</a></li>{3}", sm.SessionID, op8, op8c, hrView);
                                sm.BaseLanguage = string.Format(concent, imgResponse, sm.TNO, "T", sm.TNickname, title, GetDateTimeFormat(sp8, sm.HappenDate.AddDays(7), CurDTime), hrUrl);
                                myOpSList.Add(sm);
                            }
                            else
                            {
                                sm.BaseLanguage = string.Format(concent, imgResponse, sm.SNO, "", sm.SNickname, title, GetDateTimeFormat(sp8, sm.HappenDate.AddDays(7), CurDTime), hrView);
                                anotherOpSList.Add(sm);
                            }
                        }
                        break;
                    case "B"://学生还未确认课时,老师已确认上完课
                        if (sm.HappenDate.AddDays(7) > CurDTime)
                        {
                            sm.CountMsg = (int)((TimeSpan)(sm.HappenDate.AddDays(7) - CurDTime)).TotalMinutes;
                            if (sm.SNO == MyMemberNo)
                            {
                                //hrUrl = string.Format("<li><a href='/session/sessionstatef.htm?sid={0}'>{1}</a></li><li><a href='/session/sessionstatec.htm?sid={0}'>{2}</a></li>{3}", sm.SessionID, opB, opBc, hrView);
                                hrUrl = string.Format("<li><a href='/session/session.htm?sid={0}'>{1}</a></li><li><a href='/session/session.htm?sid={0}'>{2}</a></li>{3}", sm.SessionID, opB, opBc, hrView);
                                sm.BaseLanguage = string.Format(concent, imgResponse, sm.TNO, "T", sm.TNickname, title, GetDateTimeFormat(spB, sm.HappenDate.AddDays(7), CurDTime), hrUrl);
                                myOpSList.Add(sm);
                            }
                            else
                            {
                                sm.BaseLanguage = string.Format(concent, imgResponse, sm.SNO, "", sm.SNickname, title, GetDateTimeFormat(spB, sm.HappenDate.AddDays(7), CurDTime), hrView);
                                anotherOpSList.Add(sm);
                            }
                        }
                        break;
                    case "C"://学生申请退款 老师解释
                        if (sm.HappenDate.AddDays(14) > CurDTime)
                        {
                            sm.CountMsg = (int)((TimeSpan)(sm.HappenDate.AddDays(14) - CurDTime)).TotalMinutes;
                            if (sm.TNO == MyMemberNo)
                            {
                                //hrUrl = string.Format("<li><a href='/session/sessionstateh.htm?sid={0}'>{1}</a></li>{2}", sm.SessionID, opC, hrView);
                                hrUrl = string.Format("<li><a href='/session/session.htm?sid={0}'>{1}</a></li>{2}", sm.SessionID, opC, hrView);
                                sm.BaseLanguage = string.Format(concent, imgResponse, sm.SNO, "", sm.SNickname, title, GetDateTimeFormat(spC, sm.HappenDate.AddDays(14), CurDTime), hrUrl);
                                myOpSList.Add(sm);
                            }
                            else
                            {
                                sm.BaseLanguage = string.Format(concent, imgResponse, sm.TNO, "T", sm.TNickname, title, GetDateTimeFormat(spC, sm.HappenDate.AddDays(14), CurDTime), hrView);
                                anotherOpSList.Add(sm);
                            }
                        }
                        break;
                    case "9":
                    case "A":
                    case "D":
                        sm.CountMsg = (int)((TimeSpan)(CurDTime - sm.HappenDate)).TotalMinutes;
                        if (sm.TNO == MyMemberNo)
                            sm.BaseLanguage = string.Format(concent, "", sm.SNO, "", sm.SNickname, title, TP354, hrView);
                        else
                            sm.BaseLanguage = string.Format(concent, "", sm.TNO, "T", sm.TNickname, title, TP354, hrView);
                        otherSList.Add(sm);
                        break;
                    case "E":
                    case "F":
                        sm.CountMsg = (int)((TimeSpan)(CurDTime - sm.HappenDate)).TotalMinutes;
                        sm.TToSDesc = sm.HappenDate.AddDays(7) > CurDTime ? TP353 : TP352;
                        if (sm.TNO == MyMemberNo)
                            sm.BaseLanguage = string.Format(concent, "", sm.SNO, "", sm.SNickname, title, sm.TToSDesc, hrView);
                        else
                            sm.BaseLanguage = string.Format(concent, "", sm.TNO, "T", sm.TNickname, title, sm.TToSDesc, hrView);
                        otherSList.Add(sm);
                        break;
                    case "G":
                        sm.CountMsg = (int)((TimeSpan)(CurDTime - sm.HappenDate)).TotalMinutes;
                        if (sm.TNO == MyMemberNo)
                            sm.BaseLanguage = string.Format(concent, "", sm.SNO, "", sm.SNickname, title, TP352, hrView);
                        else
                            sm.BaseLanguage = string.Format(concent, "", sm.TNO, "T", sm.TNickname, title, TP352, hrView);
                        otherSList.Add(sm);
                        break;
                }
            }
            myOpSList.Sort(sortCompar);
            anotherOpSList.Sort(sortCompar);
            inClassSList.Sort(sortCompar);
            beClassSList.Sort(sortCompar);
            otherSList.Sort(sortCompar);

            myOpSList.AddRange(anotherOpSList);
            myOpSList.AddRange(inClassSList);
            myOpSList.AddRange(beClassSList);
            myOpSList.AddRange(otherSList);
            StringBuilder strAppend = new StringBuilder();
            int len = myOpSList.Count < 10 ? myOpSList.Count : 10;
            for (int i = 0; i < len; i++)
            {
                strAppend.Append(myOpSList[i].BaseLanguage);
            }
            return len > 0 ? strAppend.ToString() : string.Format("{0}<a href='{1}/course/'>{2}</a>", GetText("TP362"), WWWHost, GetText("TP045"));
        }
        private int sortCompar(SessionsModel sm1, SessionsModel sm2)
        {
            return sm1.CountMsg.CompareTo(sm2.CountMsg);
        }
        /// <summary>
        /// 格式化时间间隔值
        /// </summary>
        /// <param name="format">时间格式文本，例：{0} {1} ago</param>
        /// <param name="bDt">大时间</param>
        /// <param name="sdt">小时间</param>
        /// <returns>返回格式后两个时间间隔文本</returns>
        private String GetDateTimeFormat(String strFormatText, DateTime bDt, DateTime sdt)
        {
            TimeSpan ts = bDt - sdt;            
            string strDiffdate = string.Format("{0}:{1}:{2}:{3}:{4}:{5}",
             (ts.TotalDays < 365 ? "0" : ((int)(ts.TotalDays / 365)).ToString()),
             (ts.TotalDays < 30 ? "0" : ((int)(ts.TotalDays / 30)).ToString()),
             (ts.TotalDays < 1 ? "0" : ((int)ts.TotalDays).ToString()),
             (ts.TotalHours < 1 ? "0" : ((int)ts.TotalHours).ToString()),
             (ts.TotalMinutes < 0 ? "0" : ((int)ts.TotalMinutes).ToString()),
             (ts.TotalSeconds < 0 ? "0" : ((int)ts.TotalSeconds).ToString()));

            string[] a = C0033.Split(':');
            string[] b = strDiffdate.Split(':');
            if (a.Length < 6 || b.Length < 6) return strFormatText;
            if (b[0] != "0")
            {
                return strFormatText.Replace("{0}", b[0]).Replace("{1}", a[0]);
            }
            else if (b[1] != "0")
            {
                return strFormatText.Replace("{0}", b[1]).Replace("{1}", a[1]);
            }
            else if (b[2] != "0")
            {
                return strFormatText.Replace("{0}", b[2]).Replace("{1}", a[2]);
            }
            else if (b[3] != "0")
            {
                return strFormatText.Replace("{0}", b[3]).Replace("{1}", a[3]);
            }
            else if (b[4] != "0")
            {
                return strFormatText.Replace("{0}", b[4]).Replace("{1}", a[4]);
            }
            else if (b[5] != "0")
            {
                return strFormatText.Replace("{0}", b[5]).Replace("{1}", a[5]);
            }
            else
            {
                return "";
            }
        }       
    }
}